package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;

public class RegisterPage extends Utils {
    public static String url = "http://127.0.0.1:8888/register.html";

    public RegisterPage() {
        super(url);
    }

//    6.注册功能 -- 检查注册页面是否正常加载
    public void registerPageRight(){
        String title = driver.getTitle();
        assert title.equalsIgnoreCase("register");
        driver.findElement(By.cssSelector("#signUpForm > div > h2"));
        driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(5) > label > span > a"));
//        driver.quit();
    }

//    7.注册功能 -- 用户名为空，密码：123，确认密码：123
    public void registerFail1() throws IOException {
        driver.navigate().refresh();
        driver.findElement(By.cssSelector("#username")).sendKeys("");
        driver.findElement(By.cssSelector("#password")).sendKeys("123");
        driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys("123");
        driver.findElement(By.cssSelector("#policy")).click();
//        getScreenShot(getClass().getName());
        driver.findElement(By.cssSelector("#submit")).click();
//        driver.quit();
    }

//    8.注册功能 -- 密码为空，用户名：测试用户8，确认密码：123
    public void registerFail2() throws IOException {
        driver.navigate().refresh();
        driver.findElement(By.cssSelector("#username")).sendKeys("测试用户8");
        driver.findElement(By.cssSelector("#password")).sendKeys("");
        driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys("123");
        driver.findElement(By.cssSelector("#policy")).click();
//        getScreenShot(getClass().getName());
        driver.findElement(By.cssSelector("#submit")).click();
//        driver.quit();
    }

    //    9.注册功能 -- 用户名：测试用户8，密码：123，确认密码456
    public void registerFail3() throws IOException {
        driver.navigate().refresh();
        driver.findElement(By.cssSelector("#username")).sendKeys("测试用户8");
        driver.findElement(By.cssSelector("#password")).sendKeys("123");
        driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys("456");
        driver.findElement(By.cssSelector("#policy")).click();
//        getScreenShot(getClass().getName());
        driver.findElement(By.cssSelector("#submit")).click();
//        driver.quit();
    }

    //    10.注册功能 -- 正常注册，用户名：测试用户6，密码：123，确认密码：123
    public void registerSuss() throws IOException {
        driver.navigate().refresh();
        driver.findElement(By.cssSelector("#username")).sendKeys("测试用户6");
        driver.findElement(By.cssSelector("#password")).sendKeys("123");
        driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys("123");
        driver.findElement(By.cssSelector("#policy")).click();
        getScreenShot(getClass().getName());
        driver.findElement(By.cssSelector("#submit")).click();
        new WebDriverWait(driver, Duration.ofSeconds(3)).until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        alert.accept();
        getScreenShot(getClass().getName());
        driver.findElement(By.cssSelector("body > div.login-container > div > h2"));
        driver.findElement(By.cssSelector("body > div.login-container > div > div:nth-child(3) > span"));
        String title = driver.getTitle();
        assert title.equalsIgnoreCase("login");
//        driver.quit();
    }

//    11.注册功能 -- 正常注册，用户名：测试用户6，密码：123，确认密码：123
        public void checkLoginBUtton() throws IOException {
//            driver.navigate().back();
            driver.get(url);
            getScreenShot(getClass().getName());
            driver.findElement(By.cssSelector("body > div.page.page-center > div > div.text-center.text-muted.mt-3 > a")).click();
            getScreenShot(getClass().getName());
            driver.findElement(By.cssSelector("body > div.login-container > div > h2"));
            driver.findElement(By.cssSelector("body > div.login-container > div > div:nth-child(3) > span"));
            driver.quit();
        }

}
